<?php

namespace app\admin\model;

use app\common\model\MoneyLog;
use think\Model;

class User extends Model
{

    // 表名
    protected $name = 'user';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'prevtime_text',
        'logintime_text',
        'jointime_text',
	    'level_text',
	    'area_text'
    ];
    
    const LEVEL_USER = 0;
    const LEVEL_PROMOTION = 1;
    const LEVEL_SHOP = 2;
    const LEVEL_WAREHOUSE = 3;
    const LEVEL_CENTER = 4;
    const LEVEL_THEATER = 5;
    const LEVEL_HEADQUARTERS = 6;
    
    public static $levelList = [
        self::LEVEL_PROMOTION => '代言人',
	    self::LEVEL_USER => '用户',
	    self::LEVEL_SHOP => '场景店',
	    self::LEVEL_WAREHOUSE => '提货点',
	    self::LEVEL_CENTER => '发展中心',
	    self::LEVEL_THEATER => '战区',
	    self::LEVEL_HEADQUARTERS => '总部'
    ];
    
    public function getLevelTextAttr($value, $data) {
    	$value = $value ? : $data['level'];
    	$list = self::$levelList;
    	return $list[$value] ??'';
    }
	
	
	const AREA_SZ = 2;
	const AREA_OTHER = 1;
	
	public static $areaList = [
		self::AREA_OTHER => '其它省份',
		self::AREA_SZ => '广东省',
	];
	
	public function getAreaTextAttr($value, $data) {
		$value = $value ? : $data['area'];
		$list = self::$areaList;
		return $list[$value] ??'';
	}
	
    public function getOriginData()
    {
        return $this->origin;
    }

    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $changed = $row->getChangedData();
            //如果有修改密码
            if (isset($changed['password'])) {
                if ($changed['password']) {
                    $salt = \fast\Random::alnum();
                    $row->password = \app\common\library\Auth::instance()->getEncryptPassword($changed['password'], $salt);
                    $row->salt = $salt;
                } else {
                    unset($row->password);
                }
            }
        });


//        self::beforeUpdate(function ($row) {
//            $changedata = $row->getChangedData();
//            if (isset($changedata['money'])) {
//                $origin = $row->getOriginData();
//                MoneyLog::create(['user_id' => $row['id'], 'money' => $changedata['money'] - $origin['money'], 'before' => $origin['money'], 'after' => $changedata['money'], 'memo' => '管理员变更金额']);
//            }
//        });
    }

    public function getGenderList()
    {
        return ['1' => __('Male'), '0' => __('Female')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getPrevtimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['prevtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['logintime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getJointimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['jointime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPrevtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setLogintimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setJointimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function recommend() {
    	return $this->belongsTo('user', 'recommend_user_id', 'id', '', 'left')->setEagerlyType(0);
    }

}
